package cn.doitedu.hbase.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class PutDemo {

    public static void main(String[] args) throws IOException {

        Connection connection = null;
        Table table = null;
        try {
            connection = ConnectionUtils.getConnection();

            //将数据写入到Hbase中
            //获取一个表
            table = connection.getTable(TableName.valueOf("tb_users"));
            //指定rowkey
            Put put = new Put(Bytes.toBytes("u001"));
            //列族，列的标识符（key），对应列标识符的值(value)
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes(18));
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("gender"), Bytes.toBytes("男"));
            put.addColumn(Bytes.toBytes("f2"), Bytes.toBytes("province"), Bytes.toBytes("北京"));


            //写入一条数据
            table.put(put);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (table != null)
                table.close();
            if (connection != null)
                connection.close();
        }
    }

}
